package com.jialanmo.zhihudaily.ui.theme;

import com.jialanmo.zhihudaily.Application;
import com.jialanmo.zhihudaily.data.model.Theme;

import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

public final class ThemePresenter implements ThemeContract.Presenter {

    private final ThemeContract.View mView;
    private final int mId;

    public ThemePresenter(ThemeContract.View view, int id) {
        mView = view;
        mId = id;
    }

    @Override
    public void start() {
        getTheme();
    }

    @Override
    public void getTheme() {
        mView.setRefreshing(true);
        Application.getApplication(mView.getContext()).getZhihuApi()
                .getTheme(mId)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Action1<Theme>() {
                    @Override
                    public void call(Theme theme) {
                        mView.setRefreshing(false);
                        mView.showTheme(theme);
                    }
                }, new Action1<Throwable>() {
                    @Override
                    public void call(Throwable throwable) {
                        mView.setRefreshing(false);
                        mView.showMessage(throwable.getMessage());
                    }
                });
    }
}
